package com.example.administrator.daydayappmarket.software.choice.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import com.example.administrator.daydayappmarket.Contacts;
import com.example.administrator.daydayappmarket.DownloadActivity;
import com.example.administrator.daydayappmarket.R;
import com.example.administrator.daydayappmarket.SearchActivity;
import com.example.administrator.daydayappmarket.software.choice.model.SoftwareChoiceSecondBean;
import com.example.administrator.daydayappmarket.software.utils.ShowDialogShare;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

import java.text.DecimalFormat;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * TODO 这是App详细介绍页面
 */
public class SoftwareChoiceSecondActivity extends AppCompatActivity {

    @InjectView(R.id.activity_software_choice_second_body_head_iamgeveiw0)
    ImageView bodyHeadIamgeveiw0;
    @InjectView(R.id.activity_software_choice_second_body_head_textview0)
    TextView bodyHeadTextview0;
    @InjectView(R.id.activity_software_choice_second_body_head_imagview1)
    ImageView bodyHeadImagview1;
    @InjectView(R.id.activity_software_choice_second_body_head_imagview2)
    ImageView bodyHeadImagview2;
    @InjectView(R.id.activity_software_choice_second_body_layout)
    RelativeLayout bodyLayout;
    @InjectView(R.id.activity_software_choice_second_body_button)
    Button bodyButton;
    @InjectView(R.id.activity_software_choice_second_body_iamgeView10)
    ImageView bodyIamgeView10;
    @InjectView(R.id.activity_software_choice_second_body_textview11)
    TextView bodyTextview11;
    @InjectView(R.id.activity_software_choice_second_body_textview12)
    TextView bodyTextview12;
    @InjectView(R.id.activity_software_choice_second_body_textview13)
    TextView bodyTextview13;
    @InjectView(R.id.activity_software_choice_second_body_textview14)
    TextView bodyTextview14;
    @InjectView(R.id.activity_software_choice_second_body_head_layout3)
    LinearLayout bodyHeadLayout3;
    @InjectView(R.id.activity_software_choice_second_body_layout2)
    LinearLayout bodyLayout2;
    @InjectView(R.id.activity_software_choice_second_body_textview10)
    TextView bodyTextview10;
    @InjectView(R.id.activity_software_choice_second_body_layout1)
    LinearLayout bodyLayout1;
    @InjectView(R.id.activity_software_choice_second_body_HorizontalScrollView0_layout)
    LinearLayout bodyHorizontalScrollView0Layout;
    @InjectView(R.id.activity_software_choice_second_body_HorizontalScrollView0)
    HorizontalScrollView bodyHorizontalScrollView0;
    @InjectView(R.id.activity_software_choice_second_body_YingYong_textView0)
    TextView bodyYingYongTextView0;
    @InjectView(R.id.activity_software_choice_second_body_YingYong_button0)
    Button bodyYingYongButton0;
    @InjectView(R.id.activity_software_choice_second_body_YingYong_textview0)
    TextView bodyYingYongTextview0;
    @InjectView(R.id.activity_software_choice_second_body_YingYong_textview1)
    TextView bodyYingYongTextview1;
    @InjectView(R.id.activity_software_choice_second_body_YingYong_layout0)
    LinearLayout bodyYingYongLayout0;
    @InjectView(R.id.activity_software_choice_second_body_YingYong_textview2)
    TextView bodyYingYongTextview2;
    @InjectView(R.id.activity_software_choice_second_body_dajia_textview0)
    Button bodyDajiaTextview0;
    @InjectView(R.id.activity_software_choice_second_body_HorizontalScrollView1_layout)
    LinearLayout bodyHorizontalScrollView1Layout;
    @InjectView(R.id.activity_software_choice_second_body_HorizontalScrollView1)
    HorizontalScrollView bodyHorizontalScrollView1;
    @InjectView(R.id.activity_software_choice_second_body_scrollView)
    ScrollView bodyScrollView;
    @InjectView(R.id.activity_software_choice_second)
    RelativeLayout activitySoftwareChoiceSecond;

    public static final String REQUEST_SOFTWARE_CHOICE_SECOND = "software_choice_second";
    @InjectView(R.id.activity_software_choice_second_body_head_backRelative)
    RelativeLayout activitySoftwareChoiceSecondBodyHeadBackRelative;
    @InjectView(R.id.activity_software_choice_second_body_head_shareRelative)
    RelativeLayout activitySoftwareChoiceSecondBodyHeadShareRelative;
    @InjectView(R.id.activity_software_choice_second_body_head_downloadRelative)
    RelativeLayout activitySoftwareChoiceSecondBodyHeadDownloadRelative;
    @InjectView(R.id.activity_software_choice_second_body_head_searchRelative)
    RelativeLayout activitySoftwareChoiceSecondBodyHeadSearchRelative;
    @InjectView(R.id.activity_software_choice_second_body_emptyImage)
    ImageView activitySoftwareChoiceSecondBodyEmptyImage;
    @InjectView(R.id.activity_software_choice_second_body_emptyRelative)
    RelativeLayout activitySoftwareChoiceSecondBodyEmptyRelative;

    private RequestQueue requestQueue;
    private int index = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_software_choice_second);
        ButterKnife.inject(this);
        index = 0;
        Intent intent = getIntent();
        String id = intent.getStringExtra("id");


        bodyLayout.setVisibility(View.GONE);
        bodyButton.setVisibility(View.GONE);
        bodyScrollView.setVisibility(View.GONE);
        Glide.with(this).load(R.drawable.cat_loading_transparent).into(activitySoftwareChoiceSecondBodyEmptyImage);


        requestQueue = Volley.newRequestQueue(this);
        StringRequest stringRequest = new StringRequest(Request.Method.GET, Contacts.LIST_SECOND + id, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (response != null) {

                    bodyLayout.setVisibility(View.VISIBLE);
                    bodyButton.setVisibility(View.VISIBLE);
                    bodyScrollView.setVisibility(View.VISIBLE);
                    activitySoftwareChoiceSecondBodyEmptyImage.setVisibility(View.GONE);
                    activitySoftwareChoiceSecondBodyEmptyRelative.setVisibility(View.GONE);


                    Gson gson = new Gson();
                    SoftwareChoiceSecondBean softwareChoiceSecondBean = gson.fromJson(response, SoftwareChoiceSecondBean.class);
                    SoftwareChoiceSecondBean.DataBean data = softwareChoiceSecondBean.getData();

                    if (data.getIcon_url().startsWith("http")) {
                        Picasso.with(SoftwareChoiceSecondActivity.this).load(data.getIcon_url()).into(bodyIamgeView10);
                    } else {
                        Picasso.with(SoftwareChoiceSecondActivity.this).load(Contacts.FRONT_IMG + data.getIcon_url()).into(bodyIamgeView10);
                    }
                    bodyTextview11.setText(data.getName());
                    bodyTextview12.setText(data.getCategory());
                    bodyHeadTextview0.setText(data.getName());

                    double v = Double.parseDouble(data.getPackage_size()) / 1024 / 1024;
                    DecimalFormat df = new DecimalFormat("####0.00");
                    String format = df.format(v);
                    bodyTextview13.setText(format + "MB");

                    bodyTextview14.setText(Integer.parseInt(data.getDownload_count()) / 10000 + " 万人下载");
                    bodyTextview10.setText("小编点评：" + data.getReviews());
                    bodyYingYongTextview0.setText(data.getVersion_name());

                    String s = data.getCreated_at().replaceAll("-", "/");
                    String substring = s.substring(0, s.indexOf(" "));
                    bodyYingYongTextview1.setText(substring);


                    Spanned spanned = Html.fromHtml(data.getDescription());
                    bodyYingYongTextview2.setText(spanned);

                    List<String> screenshoot_urls = data.getScreenshoot_urls();
                    for (int i = 0; i < screenshoot_urls.size(); i++) {
                        View view = LayoutInflater.from(SoftwareChoiceSecondActivity.this).inflate(R.layout.item_activity_list_second_body_horiablescollview_first, null);
                        ImageView imageView = (ImageView) view.findViewById(R.id.item_lsit_solft_body_horiableView_imageview);
                        if (screenshoot_urls.get(i).startsWith("http")) {
                            Glide.with(getApplicationContext()).load(screenshoot_urls.get(i)).into(imageView);
                        } else {
                            Glide.with(getApplicationContext()).load(Contacts.FRONT_IMG + screenshoot_urls.get(i)).into(imageView);
                        }
                        view.setPadding(5, 0, 5, 0);
                        bodyHorizontalScrollView0Layout.addView(view);
                    }

                    List<SoftwareChoiceSecondBean.DataBean.RecommendAppsBean> recommend_apps = data.getRecommend_apps();
                    for (int i = 0; i < recommend_apps.size(); i++) {
                        View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_software_choice_gridview, null);
                        ImageView imageView = (ImageView) view.findViewById(R.id.item_software_choice_gridView_imageView);

                        TextView nameText = (TextView) view.findViewById(R.id.item_software_choice_gridView_nameTextView);
                        TextView personText = (TextView) view.findViewById(R.id.item_software_choice_gridView_SizeTextView);
                        final SoftwareChoiceSecondBean.DataBean.RecommendAppsBean recommendAppsBean = recommend_apps.get(i);
                        nameText.setText(recommendAppsBean.getName());
                        double v1 = Double.parseDouble(recommendAppsBean.getPackage_size()) / 1024 / 1024;
                        DecimalFormat df1 = new DecimalFormat("####0.00");
                        String format1 = df1.format(v1);
                        personText.setText(format1 + "MB");
                        if (recommendAppsBean.getIcon_url().startsWith("http")) {
                            Glide.with(getApplicationContext()).load(recommendAppsBean.getIcon_url()).into(imageView);
                        } else {
                            Glide.with(getApplicationContext()).load(Contacts.FRONT_IMG + recommendAppsBean.getIcon_url()).into(imageView);
                        }
                        view.setPadding(12, 20, 12, 5);

                        //设置点击监听事件

                        LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.item_software_choice_gridView_linearLayout);
                        linearLayout.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                String id1 = recommendAppsBean.getId();
                                Intent intent1 = new Intent(getApplicationContext(), SoftwareChoiceSecondActivity.class);
                                intent1.putExtra("id", id1);
                                startActivity(intent1);
                            }
                        });


                        bodyHorizontalScrollView1Layout.addView(view);
                    }


                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        stringRequest.setTag(REQUEST_SOFTWARE_CHOICE_SECOND);
        requestQueue.add(stringRequest);

        //应用描述右侧的按钮的点击事件
        bodyYingYongButton0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (index % 2 == 0) {
                    bodyYingYongTextview2.setMaxLines(100);
                    bodyYingYongButton0.setText("收起");
                    Drawable drawable = ContextCompat.getDrawable(getApplicationContext(), R.drawable.list_second_body_login_close);
                    drawable.setBounds(1, 1, 35, 35);
                    bodyYingYongButton0.setCompoundDrawables(null, null, drawable, null);
                } else if (index % 2 == 1) {
                    bodyYingYongTextview2.setMaxLines(2);
                    bodyYingYongButton0.setText("展开");
                    Drawable drawable = ContextCompat.getDrawable(getApplicationContext(), R.drawable.lsit_second_boty_login_open);
                    drawable.setBounds(1, 1, 35, 35);
                    bodyYingYongButton0.setCompoundDrawables(null, null, drawable, null);
                }
                index++;

            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        requestQueue.cancelAll(REQUEST_SOFTWARE_CHOICE_SECOND);
    }

    @OnClick({R.id.activity_software_choice_second_body_head_backRelative, R.id.activity_software_choice_second_body_head_shareRelative, R.id.activity_software_choice_second_body_head_downloadRelative, R.id.activity_software_choice_second_body_head_searchRelative})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.activity_software_choice_second_body_head_backRelative:
                finish();
                break;
            case R.id.activity_software_choice_second_body_head_shareRelative:

                ShowDialogShare.ShowDialog(SoftwareChoiceSecondActivity.this);


                break;
            case R.id.activity_software_choice_second_body_head_downloadRelative:
                Intent intent = new Intent(getApplicationContext(), DownloadActivity.class);
                startActivity(intent);
                break;
            case R.id.activity_software_choice_second_body_head_searchRelative:
                Intent intent1 = new Intent(getApplicationContext(), SearchActivity.class);
                startActivity(intent1);
                break;
        }
    }

}
